package com.changpei.search.controller;

import com.changpei.common.result.Result;
import com.changpei.search.pojo.ContentIndex;
import com.changpei.search.service.ContentIndexService;
import com.changpei.search.vo.ContentIndexVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 *
 * <p>Title : ContentIndex</p>
 * <p>Description : contentIndex的控制层</p>
 * @author weibiao
 * @date 2020年3月7日
 */
@RestController
@RequestMapping("/contentIndex")
@CrossOrigin
public class ContentIndexController {

    @Autowired
    private ContentIndexService contentIndexService;

    /**
     *
     * <p>Title : add</p>
     * <p>Description : 新增ContentIndex数据</p>
     * @param contentIndex
     * @return
     */
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody(required = false) ContentIndex contentIndex) {
        // 调用contentIndexService实现添加ContentIndex
        return contentIndexService.save(contentIndex);
    }

    /**
     *
     * <p>Title : delete</p>
     * <p>Description : ContentIndex根据ID主键删除数据</p>
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable(required = false) Long id) {
        // 调用contentIndexService实现根据主键删除
        return contentIndexService.deleteById(id);
    }

    /**
     *
     * <p>Title : update</p>
     * <p>Description : ContentIndex根据ID修改</p>
     * @param contentIndex
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody ContentIndex contentIndex, @PathVariable Long id) {
        // 设置主键值
        contentIndex.setId(id);
        // 调用contentIndexService实现修改ContentIndex
        return contentIndexService.update(contentIndex);
    }

    /**
     *
     * <p>Title : findById</p>
     * <p>Description : ContentIndex分页搜索实现</p>
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<ContentIndex> findById(@PathVariable Long id) {
        ContentIndex contentIndex = contentIndexService.findById(id);
        return Result.ok(contentIndex);
    }

    /**
     * 查询我的浏览记录
     * @param map
     * @return
     */
    @PostMapping(value = "/history")
    public Result<Map<String, Object>> history(@RequestBody Map<String, Object> map) {
        Map<String, Object> result = contentIndexService.history(map);
        return Result.ok(result);
    }

    /**
     * 查询我的收藏
     * @param map
     * @return
     */
    @PostMapping(value = "/collect")
    public Result<Map<String, Object>> collect(@RequestBody Map<String, Object> map) {
        Map<String, Object> result = contentIndexService.collect(map);
        return Result.ok(result);
    }

    /**
     * 内容详情页面推荐
     * @param map
     * @return
     */
    @PostMapping(value = "/detailRecommend")
    public Result<Set<ContentIndexVO>> detailRecommend(@RequestBody Map<String, Object> map) {
        Set<ContentIndexVO> result = contentIndexService.detailRecommend(map);
        return Result.ok(result);
    }

    /**
     * 查询用户下内容
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/user/{userId}")
    public Result<Page<ContentIndexVO>> getUserContents(@PathVariable Long userId,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "50") Integer pageSize) {
        Page<ContentIndexVO> userContents = contentIndexService.getUserContents(userId, pageNum, pageSize);
        return Result.ok(userContents);
    }

    /**
     * 搜索页面
     * @param content
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/search")
    public Result<Page<ContentIndexVO>> search(@RequestParam(value = "content") String content,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "50") Integer pageSize) {
        Page<ContentIndexVO> userContents = contentIndexService.search(content, pageNum, pageSize);
        return Result.ok(userContents);
    }

}
